#include "vebi.h"
int r_xne[]={1,0,-1,1,-1,0,0,-1,1};
int r_yne[]={1,0,-1,1,-1,0,0,-1,1};
Vebi::Vebi()
{
}
Vebi::~Vebi(){

}
void Vebi::init(const Mat _image){

samples=new unsigned char **[rows];
for(int i=0;i<rows;i++){
    samples[i]=new unsigned char *[cols];
    for(int j=0;j<cols;j++){
        samples[i][j]=new unsigned char[NUM_YB+1];
        for(int k=0;k<NUM_YB+1;k++){
            samples[i][j][k]=0;
        }
    }
}
m_mask=Mat::zeros(_image.size(),CV_8UC1);
}

void Vebi::initEx(const Mat _image){




        for(int k=0;k<NUM_YB;k++){
            sam[k]=Mat::zeros(_image.size(),CV_8UC1);
        }
m_mask=Mat::zeros(_image.size(),CV_8UC1);
}
void Vebi::displayYangBen(){
    for(int i=0;i<rows;i++){
        for(int j=0;j<cols;j++){
            cout<<"samples["<<i<<"]["<<j<<"]={";
                for(int k=0;k<NUM_YB+1;k++){
                        if(k<NUM_YB){
                        cout<<(int)samples[i][j][k]<<",";
                        }else{
                            cout<<(int)samples[i][j][k];
                        }
                }
                cout<<"}   ";

        }
         cout<<endl;
    }
}
void Vebi::executeFirstFrame(const Mat _image){
    rows=_image.rows;
    cols=_image.cols;
    RNG rng;
    int _rows=0;
    int _cols=0;
    init(_image);

    for(int i=0;i<rows;i++){

        for(int j=0;j<cols;j++){
            for(int k=0;k<NUM_YB;k++){
                    int ramd=rng.uniform(0,9);
                    _rows=i+r_yne[ramd];
                    if(_rows<0){
                        _rows=0;
                    }
                    if(_rows>rows-1){
                        _rows=rows-1;
                    }
                    _cols=j+r_xne[ramd];
                    if(_cols<0){
                        _cols=0;
                    }
                    if(_cols>cols-1){
                        _cols=cols-1;
                    }
                 samples[i][j][k]=_image.at<uchar>(_rows,_cols);
            }
        }
    }

}
void Vebi::executeFirstFrameEx(const Mat _image)
{
    rows=_image.rows;
    cols=_image.cols;
    RNG rng;
    int _rows=0;
    int _cols=0;
     initEx(_image);

     for(int i=0;i<rows;i++){

         for(int j=0;j<cols;j++){
             for(int k=0;k<NUM_YB;k++){
                     int ramd=rng.uniform(0,9);
                     _rows=i+r_yne[ramd];
                     if(_rows<0){
                         _rows=0;
                     }
                     if(_rows>rows-1){
                         _rows=rows-1;
                     }
                     _cols=j+r_xne[ramd];
                     if(_cols<0){
                         _cols=0;
                     }
                     if(_cols>cols-1){
                         _cols=cols-1;
                     }
                  sam[k].at<uchar>(i,j)=_image.at<uchar>(_rows,_cols);
             }
         }
     }

}
void Vebi::checkandUpdate(const Mat _image){
    rows=_image.rows;
    cols=_image.cols;
    RNG rng;
for(int i=0;i<rows;i++){
    for(int j=0;j<cols;j++){
           int matchs(0),count(0);
           float dis=0;
            while(count<NUM_YB && matchs<MIN_MTCH){
                    dis=abs(samples[i][j][count]-_image.at<uchar>(i,j));
                    if(dis<RDIU)
                        matchs++;
                    count++;
            }
            if(matchs>=MIN_MTCH){
                    samples[i][j][NUM_YB]=0;
                    m_mask.at<uchar>(i,j)=0;
                    int rnd=rng.uniform(0,GLV);
                    if(rnd==0){
                        rnd=rng.uniform(0,NUM_YB);
                        samples[i][j][rnd]=_image.at<uchar>(i,j);
                    }
                    rnd=rng.uniform(0,GLV);
                    if(rnd==0){
                        int _cols(0),_rows(0);
                        rnd=rng.uniform(0,9);
                        _rows=i+r_yne[rnd];
                        if(_rows<0){
                            _rows=0;
                        }
                        if(_rows>rows-1){
                            _rows=rows-1;
                        }
                        rnd=rng.uniform(0,9);
                        _cols=j+r_xne[rnd];
                        if(_cols<0){
                            _cols=0;
                        }
                        if(_cols>cols-1){
                            _cols=cols-1;
                        }
                        rnd=rng.uniform(0,NUM_YB);
                        samples[_rows][_cols][rnd]=_image.at<uchar>(i,j);


                    }
            }else{
                samples[i][j][NUM_YB]++;
                m_mask.at<uchar>(i,j)=255;
                if(samples[i][j][NUM_YB]>MJ){
                        int rnd=rng.uniform(0,GLV);
                        if(rnd==0){
                                rnd=rng.uniform(0,NUM_YB);
                                samples[i][j][rnd]=_image.at<uchar>(i,j);

                        }
                }

            }


    }
}
}
